//
//  BaseViewController.m
//  mike2.0
//
//  Created by mike on 15/9/7.
//  Copyright (c) 2015年 flychi. All rights reserved.
//

#import "BaseViewController.h"
@interface BaseViewController ()

/**
 *  load背景页面
 */
@property (nonatomic, strong) UIView *loadBackgroundView;

/**
 *  通知
 */
@property (nonatomic, strong) NSMutableArray *notificationCenterObservers;


@end

@implementation BaseViewController

#pragma mark - Life cycle

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = LCOLOROFNAV;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:iPhone5?20:22]}];
    self.view.backgroundColor = [UIColor whiteColor];
    //兼容iOS7
    [self.navigationController.navigationBar setTranslucent:NO];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self loadDefaultDataSource];
    [self initComponents];
    [self initViews];
}

- (void)backItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    [self deallocRelease];
}

- (void)deallocRelease{
    //取消请求
    [_requestOperationManager.operationQueue cancelAllOperations];
    _requestOperationManager=nil;
    //取消通知
    for (id observer in self.notificationCenterObservers){
        [[NSNotificationCenter defaultCenter] removeObserver:observer];
    }
}

#pragma mark - Property
- (MBProgressHUD *)HUD{
    if (!_HUD) {
        _HUD = [[MBProgressHUD alloc] initWithView:self.view];
//        _HUD.label.text = @"正在加载";
        [self.view addSubview:_HUD];
    }
    return _HUD;
}
- (AFHTTPSessionManager *)requestOperationManager{
    if (!_requestOperationManager) {
        _requestOperationManager = [HttpRequest requestOperationManager];
    }
    return _requestOperationManager;
}
-(UIView *)loadBackgroundView{
    if (!_loadBackgroundView) {
        _loadBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }
    return _loadBackgroundView;
}

-(NSMutableArray *)notificationCenterObservers{
    if (!_notificationCenterObservers) {
        _notificationCenterObservers = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _notificationCenterObservers;
}

#pragma mark - SubViews UI

- (void)initViews {
    self.view.backgroundColor=[UIColor whiteColor];
    // subClass
}

- (void)initComponents{
    // subClass
}

#pragma mark - Data Source

- (void)loadDefaultDataSource {
    // subClass
}

- (void)loadDataSource {
    // subClass
}

#pragma mark - View Rotation

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

#pragma mark - Public Methods

- (void)configuraEdgesForExtendedLayout {
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

- (void)willPopViewController{
    // subClass
}

- (void)showLoadingWithClear:(BOOL)isClear{
    [self.view addSubview:self.loadBackgroundView];
    [self.HUD showAnimated:YES];
    if (isClear) {
        _loadBackgroundView.backgroundColor=[UIColor clearColor];
    }else{
        _loadBackgroundView.backgroundColor=[UIColor whiteColor];
    }
}

- (void)hideLoading{
    [self.HUD hideAnimated:NO];
    [_loadBackgroundView removeFromSuperview];
}

- (void)addNotification:(NSString *)notification block:(NotificationBlock)block{
    id observer = [[NSNotificationCenter defaultCenter] addObserverForName:notification
                                                                    object:nil
                                                                     queue:[NSOperationQueue mainQueue]
                                                                usingBlock:block];
    [self.notificationCenterObservers addObject:observer];
}
-(UILabel *)vc_nolabel{
    if (!_vc_nolabel) {
        _vc_nolabel = [UILabel new];
        _vc_nolabel.text = @"暂无内容";
        _vc_nolabel.textColor = XFUIColorFromRGB(0x999999);
        _vc_nolabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_vc_nolabel];
        //        [self.view bringSubviewToFront: _tab_nolabel];
        [_vc_nolabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.top.mas_equalTo(0.5 * (SCREEN_HEIGHT - 35 - 8.3 * GRIDH));
            make.height.mas_equalTo(50);
        }];
    }
    return _vc_nolabel;
}

-(UILabel *)vc_searchNoLabel{
    if (!_vc_searchNoLabel) {
        _vc_searchNoLabel =[UILabel new];
        _vc_searchNoLabel.text = @"没有找到搜索结果喔~";
        _vc_searchNoLabel.textColor = XFUIColorFromRGB(0x999999);
        _vc_searchNoLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_vc_searchNoLabel];
        [_vc_searchNoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.top.mas_equalTo(60);
            make.height.mas_equalTo(50);
        }];
    }
    return _vc_searchNoLabel;
}
@end
